//
//  IWantToTakeTheClothesViewController.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/10.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "IWantToTakeTheClothesViewController.h"
#import "IWantToTakeTheClothesTableViewCell.h"
#import "PickUpDetailsViewController.h"

@interface IWantToTakeTheClothesViewController ()<UITableViewDelegate , UITableViewDataSource>

Strong UITableView *tableView;
Strong NSMutableArray *dataArray; /**<*/
Assign int pstart; //页数

@end

//网络
@implementation IWantToTakeTheClothesViewController(NetWorking)

- (void)getData:(NSString *)page{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"page":page,
                                 @"pageSize":@"10",
                                 @"uId":[UserModel sharedInstanced].userId
                                 };
    [[AFAppDotNetAPIClient sharedClient] POST:@"wardrobe/Laundry_Order/getUnreciveOrderInfo.do" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            if ([page isEqualToString:@"0"]) {
                [self.dataArray removeAllObjects];
                self.pstart = 0;
            }
            [self.dataArray addObjectsFromArray:responseObject[@"data"]];
            [self.tableView reloadData];
            self.pstart ++;
        }else{
            ShowMessage(responseObject[@"msg"]);
            [self.dataArray removeAllObjects];
            [self.tableView reloadData];
            self.pstart = 0;
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showError:error];
        [self hiddenProgressHUD];
    }];
}

@end


//ui
@implementation IWantToTakeTheClothesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我要取衣";
    [self.view addSubview:self.tableView];
    [self getData:@"0"];
}

#pragma mark - init

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsZero;
        if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [_tableView setLayoutMargins:UIEdgeInsetsZero];
        }
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [UIView new];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getData:@"0"];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self getData:[NSString stringWithFormat:@"%d",self.pstart]];
        }];
    }
    return _tableView;
}


#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IWantToTakeTheClothesTableViewCell *cell = [IWantToTakeTheClothesTableViewCell cellForTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    //cell.accessoryType = indexPath.row == 3 ?  UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PickUpDetailsViewController *vc = [PickUpDetailsViewController new];
    vc.dataDictonary = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 87 / WIDTH_5S_SCALE;
}


#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[
                        ].mutableCopy;
    }
    return _dataArray;
}

@end
